package com.example.mybatis.rest;

import com.example.mybatis.dao.BookkeepingDao;
import com.example.mybatis.moudle.Bookkeeping;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 阿恩
 * @date 2020/11/3010:23
 */
@RestController
@RequestMapping("/bookkeeping")
public class BookkeepingRest {
    @Autowired
    SqlSessionFactory factory;


    /**
     * 根据主键删除一条记录
     * http://localhost:9090/bookkeeping/deleteById/
     * @param id
     */
    @GetMapping("deleteById/{id}")
    public void deleteByPrimaryKey(@PathVariable("id") int id){
        SqlSession sess=factory.openSession();
        BookkeepingDao dao=sess.getMapper(BookkeepingDao.class);
        dao.deleteByPrimaryKey(id);
        sess.commit();
        sess.close();
    }

    /**
     * 输出所有
     * http://localhost:9090/bookkeeping/findAll
     * @return
     */
    @GetMapping("findAll")
    public List<Bookkeeping> findAll(){
        SqlSession sess=factory.openSession();
        BookkeepingDao dao=sess.getMapper(BookkeepingDao.class);
        List<Bookkeeping> list=dao.findAll();
        System.out.println(list);
        sess.close();
        return list;
    }

    /**
     * 输出支出2/收入1
     * http://localhost:9090/bookkeeping/findByFlag/1
     * @return
     */
    @GetMapping("findByFlag/{flag}")
    public List<Bookkeeping> findByFlag(@PathVariable("flag")int flag){
        SqlSession sess=factory.openSession();
        BookkeepingDao dao=sess.getMapper(BookkeepingDao.class);
        List<Bookkeeping> list=dao.findByFlag(flag);
        System.out.println(list);
        sess.close();
        return list;
    }

    /**
     * 按主键查找
     * http://localhost:9090/bookkeeping/selectById/1
     * @param outputId 主键
     * @return
     */
    @GetMapping("selectById/{id}")
    public Bookkeeping selectById(@PathVariable("id") int outputId){
        SqlSession sess=factory.openSession();
        BookkeepingDao dao=sess.getMapper(BookkeepingDao.class);
        Bookkeeping bookkeeping=dao.selectByPrimaryKey(outputId);
        sess.close();
        return bookkeeping;
    }

    /**
     * 按月查找
     *http://localhost:9090/bookkeeping/selectByMonth/2020/11
     * @param month
     * @return
     */
    @GetMapping("selectByMonth/{year}/{month}")
    public List<Bookkeeping> selectByMonth(@PathVariable("year") int year,@PathVariable("month") int month){
        SqlSession sess=factory.openSession();
        BookkeepingDao dao=sess.getMapper(BookkeepingDao.class);
        List<Bookkeeping> list=dao.selectByMonth(year,month);
        sess.close();
        return list;
    }
    /**
     * 按月查找收入/支出
     *http://localhost:9090/bookkeeping/selectByMonth/2020/11/1
     * @param month
     * @return
     */
    @GetMapping("selectByMonth/{year}/{month}/{flag}")
    public List<Bookkeeping> selectByMonth(@PathVariable("year") int year,@PathVariable("month") int month,@PathVariable("flag")int flag){
        SqlSession sess=factory.openSession();
        BookkeepingDao dao=sess.getMapper(BookkeepingDao.class);
        List<Bookkeeping> list=dao.selectByMonthFlag(year,month,flag);
        sess.close();
        return list;
    }

    /**
     * 按年查找
     * http://localhost:9090/bookkeeping/selectByYear/2020
     * @param year
     * @return
     */
    @GetMapping("selectByYear/{year}")
    public List<Bookkeeping> selectByYear(@PathVariable("year") int year){
        SqlSession sess=factory.openSession();
        BookkeepingDao dao=sess.getMapper(BookkeepingDao.class);
        List<Bookkeeping> list=dao.selectByYear(year);
        sess.close();
        return list;
    }

    /**
     * 按照支付方式(支付宝,微信...)查找
     * http://localhost:9090/bookkeeping/selectByMethod/微信
     * @param method
     * @return
     */
    @GetMapping("selectByMethod/{method}")
    public List<Bookkeeping> selectByMethod(@PathVariable("method") String method){
        SqlSession sess=factory.openSession();
        BookkeepingDao dao=sess.getMapper(BookkeepingDao.class);
        List<Bookkeeping> list=dao.selectByMethod(method);
        sess.close();
        return list;
    }

    /**
     * 按照支付方式(支付宝,微信...)查找
     * http://localhost:9090/bookkeeping/selectByMethod/微信/1
     * @param method
     * @param flag 1收入,2支出
     * @return
     */
    @GetMapping("selectByMethod/{method}/{flag}")
    public List<Bookkeeping> selectByMethod(@PathVariable("method") String method,@PathVariable("flag")int flag){
        SqlSession sess=factory.openSession();
        BookkeepingDao dao=sess.getMapper(BookkeepingDao.class);
        List<Bookkeeping> list=dao.selectByMethodFlag(method,flag);
        sess.close();
        return list;
    }

    /**
     * 按种类(零食,水果...)查找
     * http://localhost:9090/bookkeeping/selectByKind/电费
     * @param kind
     * @return
     */
    @GetMapping("selectByKind/{kind}")
    public List<Bookkeeping> selectByKind(@PathVariable("kind") String kind){
        SqlSession sess=factory.openSession();
        BookkeepingDao dao=sess.getMapper(BookkeepingDao.class);
        List<Bookkeeping> list=dao.selectByKind(kind);
        sess.close();
        return list;
    }

    /**
     * 新增一条记录
     * http://localhost:9090/bookkeeping/insert
     * @param map
     * @return
     */
    @PostMapping("/insert")
    @ResponseBody
    public Bookkeeping insert(@RequestBody Map map) {
        try {
//            System.out.println("map:"+map);
            String id=map.get("id").toString();
            String count=map.get("count").toString();
            String date=map.get("date").toString();
            Date date1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date);
            String kind=map.get("kind").toString();
            String method=map.get("method").toString();
            String text=map.get("text").toString();
            String flag=map.get("flag").toString();
            Bookkeeping bookkeeping = new Bookkeeping(Integer.parseInt(id),
                    Double.parseDouble(count),
                    date1,
                    kind, method, text,Integer.parseInt(flag));
//            System.out.println("数据:"+bookkeeping);
            SqlSession sess=factory.openSession();
            BookkeepingDao dao=sess.getMapper(BookkeepingDao.class);
            dao.insertSelective(bookkeeping);
            sess.commit();
            sess.close();
            System.out.println(bookkeeping);
            return bookkeeping;
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        finally {
            return null;
        }


    }


    /**
     * 根据主键更新一条记录
     * http://localhost:9090/bookkeeping/updat
     * @param bookkeeping
     * @return
     */
    @PostMapping("/update")
    @ResponseBody
    public Bookkeeping update(@RequestBody Bookkeeping bookkeeping) {
        try {
            System.out.println(bookkeeping);
            SqlSession sess=factory.openSession();
            BookkeepingDao dao=sess.getMapper(BookkeepingDao.class);
            dao.updateByPrimaryKeySelective(bookkeeping);
            sess.commit();
            sess.close();
            return bookkeeping;
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        finally {
            return null;
        }


    }
}
